﻿//============================================================
//Tree
//============================================================
function Tree() {
    var _this = this;
    _this.ID = ""; //树生成时的ID
    _this.TextClickType = "click"; //click/expand/
    _this.TextClickFun = null; //单机文本时响应的方法名称
    //_this.TextDbClickFun = null;
    _this.ReturnType = "value"; //value/text/text:value
    _this.SelectAllChild = true; //单机父节点时是否选中它所有的子节点
    //_this.SelectChild = false;
    //_this.CheckBoxClickFun = null;
    _this.ImgWidth = "16px"; //默认图片宽度
    _this.ImgHeight = "16px"; //默认图片高度
    _this.SetNodeTextAsNodeTitle = true;

    var MaxWidth = 0;

    _this.initTree = function (itemList, parentID) {
        window.status = "";
        InitTreeParent(itemList, parentID, true);

        var expendedNode = getPageSessionStorage(_this.ID + "Expended");
        if (expendedNode && expendedNode != null) {
            expendExpendedNode(expendedNode);
        }
        var nodeSelected = getPageSessionStorage(_this.ID + "NodeSelected");
        if (nodeSelected && nodeSelected != null) {
            selectSelectedNode(nodeSelected);
        }
        $(window).unload(function () {
            removePageSessionStorageByKey(_this.ID + "Expended");
            removePageSessionStorageByKey(_this.ID + "NodeSelected");
            window.status = "";
        });

        if ($("#" + _this.ID).width() < MaxWidth)
            $("#" + _this.ID).width(MaxWidth);
    }

    var selectSelectedNode = function (selectedid) {
        var node = $("#" + selectedid);
        var iCount = node.length;
        if (iCount == 0) {
            return;
        } else if (iCount == 1) {
            node.children(".treeNode_text").addClass("treeNodeSelected");
        } else {
            selectSelectedNode(selectedid.substring(0, selectedid.length - 2));
        }
    }

    var expendExpendedNode = function (expendedid) {
        var node = $("#plus_" + expendedid);
        if (node.hasClass("treeNode_plus")) {
            var ul = $("#" + expendedid).children("ul");
            node.addClass("treeNode_minus").removeClass("treeNode_plus");
            ul.show();
        }
        if (expendedid.split('_').length > 2) {
            expendExpendedNode(expendedid.substring(0, expendedid.length - 2));
        }
    }
    _this.getSelectNode = function () {

        checkList = ""; //初始化已选项列表
        var firstNodeID = _this.ID + "_0";
        //判断根节点是否被选中
        if ($("#cb_" + firstNodeID).prop("checked") == true) {
            if (_this.ReturnType == 'value') {
                checkList += $("#" + firstNodeID).attr("nodevalue") + ",";
            }
            else if (_this.ReturnType == 'text') {
                checkList += $("#" + firstNodeID).attr("nodetext") + ",";
            }
            else if (_this.ReturnType == 'text:value') {
                checkList += $("#" + firstNodeID).attr("nodetext") + ":" + $("#" + firstNodeID).attr("nodevalue") + ",";
            }
        }

        GetSelectChild($("#" + firstNodeID));
        return checkList;
    }

    var InitTreeParent = function (itemList, parentID, expand) {
        var vTree = $("<ul/>");
        if (parentID != "")
            $(vTree).css("margin-left", "16px");

        if (parentID == "") {
            $("#" + _this.ID).append(vTree);
        }
        else {
            $("#" + parentID).append(vTree);
            initExpand(vTree, expand);
        }

        if (itemList.ChildNodes.length > 0) {
            for (var ii = 0; ii < itemList.ChildNodes.length; ii++) {
                var childID = parentID + "_" + ii;
                if (parentID == "")
                    childID = _this.ID + "_" + ii;

                var childnode = itemList.ChildNodes[ii];
                //-----------------------
                //预计本结点的长度
                var vNodeWidth = (15 * childnode.Text.length) + (16 * childID.split('_').length);
                if (vNodeWidth > MaxWidth)
                    MaxWidth = vNodeWidth;

                //-----------------------
                var title = _this.SetNodeTextAsNodeTitle ? childnode.Text : childnode.Title;
                var vNode = $("<li/>").addClass("treeNode").attr("id", childID).attr("title", title).attr("nodetext", childnode.Text).attr("nodevalue", childnode.Value).attr("nodeobj", childnode.Obj);
                var vPlus = $("<span/>").attr("id", "plus_" + childID);
                if (childnode.ChildNodes.length > 0) {
                    $(vPlus).click(function () {
                        PlusClick(this);
                    });
                    $(vPlus).addClass("treeNode_minus");
                }
                else {
                    $(vPlus).addClass("treeNode_blank");
                }
                $(vNode).append(vPlus);
                //------------------------
                if (childnode.ShowCheckBox) {
                    var vChk = $("<input type='checkbox' style='margin-right:4px;vertical-align:bottom;'/>").attr("id", "cb_" + childID);
                    var vChkSpan = $("<span />").css("display", "table-cell").css("vertical-align", "bottom").append(vChk);
                    if (childnode.Checked) {
                        $(vChk).prop("checked",true);
                    }
                    $(vNode).append(vChkSpan);

                    $(vChk).click(function () {
                        CheckBoxClick(this);
                    });
                }

                //------------------------
                if (childnode.ImageUrl != null) {
                    var vImg = $("<img style='margin-right:4px;'/>").attr("src", getRootPath() + childnode.ImageUrl).attr("width", _this.ImgWidth).attr("height", _this.ImgHeight);
                    var vImgSpan = $("<span/>").css("display", "table-cell").css("vertical-align", "bottom").append(vImg);
                    $(vNode).append(vImgSpan);
                }

                //------------------------
                var vText = $("<span/>").addClass("treeNode_text").text(childnode.Text);
                if (_this.TextClickType == "expand") {
                    $(vText).css("cursor", "pointer");
                }
                else if (_this.TextClickType == "click") {
                    $(vText).css("cursor", "default");
                }

                vText.click(function () {
                    if (_this.TextClickType == "expand") {
                        PlusClick(this);
                    }
                    if (_this.TextClickFun != null && typeof (_this.TextClickFun) != "undefined") {
                        _this.TextClickFun($(this).parent("li"));
                    }

                    $(".treeNodeSelected").removeClass("treeNodeSelected");
                    var $this = $(this);
                    setPageSessionStorage(_this.ID + "NodeSelected", $this.parent().attr("id"));
                    $this.addClass("treeNodeSelected");
                    var dataOnclick = $this.attr("data-onclick");
                    window.status = $this.text();
                    if (dataOnclick) {
                        eval(dataOnclick);
                    }
                    return false;

                });

                if (childnode.ClientFunName != null && childnode.ClientFunName != "") {
                    if (childnode.ClientFunName.indexOf("(") > -1) {
                        $(vText).attr("data-onclick", childnode.ClientFunName);
                    } else {
                        $(vText).attr("onclick", childnode.ClientFunName + "(this);")
                    }
                }

                $(vNode).append(vText);
                $(vTree).append(vNode);

                InitTreeParent(childnode, childID, childnode.Expanded);
            }
        }
    }

    var PlusClick = function (vObj) {
        var vLi = $(vObj).parent("li");
        var vUl = $(vLi).children("ul");
        var vID = $(vLi).attr("id");
        var vPlus = $(vLi).children("#plus_" + vID);
        $(vUl).toggle();
        if ($(vUl).css("display") == "none") {
            $(vPlus).addClass("treeNode_plus");
            $(vPlus).removeClass("treeNode_minus");
        }
        else {
            $(vPlus).removeClass("treeNode_plus");
            $(vPlus).addClass("treeNode_minus");
            setPageSessionStorage(_this.ID + "Expended", vID);
        }
    }

    function initExpand(vObj, expand) {
        var vLi = $(vObj).parent("li");
        var vUl = $(vLi).children("ul");
        var vID = $(vLi).attr("id");
        var vPlus = $(vLi).children("#plus_" + vID);

        if (expand) {
            $(vPlus).removeClass("treeNode_plus");
            $(vPlus).addClass("treeNode_minus");
            $(vObj).show();
        }
        else {
            $(vPlus).addClass("treeNode_plus");
            $(vPlus).removeClass("treeNode_minus");
            $(vObj).hide();
        }
    }

    var CheckBoxClick = function (sel) {
        if (_this.SelectAllChild) {
            var vID = $(sel).attr("id").replace('cb_', '')
            var vLi = $("#" + vID);
            var vUl = $(vLi).children("ul");

            if ($(sel).prop("checked")) {
                $(vUl).find(":checkbox").prop("checked", "true");
            }
            else {
                $(vUl).find(":checkbox").removeAttr("checked");
            }
        }
    }

    var GetSelectChild = function (vLi) {
        var col = $(vLi).children("ul").children("li");
        for (var ii = 0; ii < col.length; ii++) {
            var vID = $(col[ii]).attr("id");
            if ($("#cb_" + vID).prop("checked") == true) {
                if (_this.ReturnType == 'value') {
                    checkList += $("#" + vID).attr("nodevalue") + ",";
                }
                else if (_this.ReturnType == 'text') {
                    checkList += $("#" + vID).attr("nodetext") + ",";
                }
                else if (_this.ReturnType == 'text:value') {
                    checkList += $("#" + vID).attr("nodetext") + ":" + $("#" + vID).attr("nodevalue") + ",";
                }
            }

            if ($("#" + vID).children("ul").length != 0)
                GetSelectChild($("#" + vID));
        }
    }
}